package io.github.syske.springbootjdbcmybatisdemo.common;

import io.github.syske.springbootjdbcmybatisdemo.entity.TempOp;
import org.springframework.stereotype.Service;

import java.sql.*;

@Service
public class DBDirectorService {

    public static final String url = "jdbc:oracle:thin:@127.0.0.1:1521/MWMS";
    public static final String user = "std_mwms_whqr";
    public static final String password = "hsbk_feng_whqr";

    public int saveRealTempOP(TempOp tempOp) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(url, user, password);
            String sql = "INSERT INTO TEMP_OP " +
                    "  (SYS_PK, LOCATE, OP_DESC, M_CODE,  OP_MAN, OP_CODE," +
                    "  OP_DT,OP_TYPE,STATION,KF_CODE,OP_NUM)" +
                    "  VALUES(?, ?, ?, ?, ?, ?," +
                    "  ?,?,?,?,?)";
            statement = connection.prepareCall(sql);
            statement.setString(1, tempOp.sys_code);
            statement.setString(2, tempOp.locate);
            statement.setString(3, "TP");
            statement.setString(4, tempOp.m_code);
            statement.setString(5, "马媛媛");
            statement.setString(6, tempOp.op_code);
            statement.setDate(7, new Date(new java.util.Date().getTime()));
            statement.setString(8, "TPIN");
            statement.setString(9, tempOp.stt_code);
            statement.setString(10, "0003");
            System.out.println("op_num是:"+tempOp.op_num);
            if(tempOp!=null && !tempOp.equals("")){
                statement.setString(11, tempOp.op_num);
            }else {
                statement.setString(11, "0");
            }
            return statement.executeUpdate();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        return 0;
    }

    public int saveOUTTempOP(TempOp tempOp,String op_type,String op_desc) {

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(url, user, password);
            String sql = "INSERT INTO TEMP_OP " +
                    "  (SYS_PK, LOCATE, OP_DESC, M_CODE,  OP_MAN, OP_CODE," +
                    "  OP_DT,OP_TYPE,STATION,KF_CODE,OP_NUM,M_NUM)" +
                    "  VALUES(?, ?, ?, ?, ?, ?," +
                    "  ?,?,?,?,?,?)";
            statement = connection.prepareCall(sql);
            statement.setString(1, tempOp.sys_code);
            statement.setString(2, tempOp.locate);
            statement.setString(3, op_desc);
            System.out.println("整盘出库的m_code是:"+tempOp.m_code);
            statement.setString(4, tempOp.m_code);
            statement.setString(5, "马媛媛");
            statement.setString(6, tempOp.op_code);
            statement.setDate(7, new Date(new java.util.Date().getTime()));
            statement.setString(8, op_type);
            statement.setString(9, tempOp.stt_code);
            statement.setString(10, "0003");
            System.out.println("op_num是:"+tempOp.op_num);
            if(tempOp!=null && !tempOp.equals("")){
                statement.setString(11, tempOp.op_num);
            }else {
                statement.setString(11, "0");
            }

            if(tempOp!=null && !tempOp.equals("")){
                statement.setString(12, tempOp.op_num);
            }else {
                statement.setString(12, "0");
            }

            return statement.executeUpdate();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        return 0;
    }

}
